package com.finley.flowabledemo.plc.service;

import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ModbusService {

    private final ModbusTcp modbusTcp;

    public ModbusService(ModbusTcp modbusTcp) {
        this.modbusTcp = modbusTcp;
    }

    /**
     * 读取线圈状态
     * @param address 起始地址
     * @param quantity 读取数量
     * @return 线圈状态列表
     */
    public List<Boolean> readCoils(int address, int quantity) {
        return modbusTcp.readCoil(address, quantity);
    }

    /**
     * 读取保持寄存器(原始字节)
     * @param address 起始地址
     * @param quantity 读取数量
     * @return 寄存器字节数据
     */
    public byte[] readHoldRegisters(int address, int quantity) {
        return modbusTcp.readHoldRegister(address, quantity);
    }

    /**
     * 读取保持寄存器(16位有符号整数)
     * @param address 寄存器地址
     * @return 16位有符号整数
     */
    public short readInt16(int address) {
        return modbusTcp.readInt16(address);
    }

    /**
     * 读取保持寄存器(32位浮点数)
     * @param address 寄存器地址
     * @return 32位浮点数
     */
    public float readFloat32(int address) {
        return modbusTcp.readFloat32(address);
    }

    /**
     * 写入单个线圈
     * @param address 线圈地址
     * @param value 要写入的值
     */
    public void writeCoil(int address, boolean value) {
        modbusTcp.writeCoil(address, value);
    }

    /**
     * 写入单个寄存器(16位有符号整数)
     * @param address 寄存器地址
     * @param value 要写入的值
     */
    public void writeInt16(int address, short value) {
        modbusTcp.writeInt16(address, value);
    }

    /**
     * 写入单个寄存器(32位浮点数)
     * @param address 寄存器地址
     * @param value 要写入的值
     */
    public void writeFloat32(int address, float value) {
        modbusTcp.writeFloat32(address, value);
    }
}